package com.sdry.web.controller.llm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Page;
import com.sdry.model.llm.RoleBean;
import com.sdry.model.llm.User;
import com.sdry.service.llm.RoleService;
import com.sdry.service.llm.UserService;
import com.sdry.utils.MD5;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/** 
* 用户
* @author lian 
* @version 1.0
* @time 2018年10月17日 上午12:48:09
*/

@Controller
@RequestMapping("/userMana")
public class UserController {

	@Resource
	private UserService userService;  //注入用户service
	@Resource
	private RoleService roleService;  //注入角色Service
	
	/**
	 * 分页查询用户信息
	 * @param page 分页条件
	 * @param user 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageUser")
	public void listPageUser(Page page, User user, HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (user.getUserName() != null ) {
			map.put("userName", user.getUserName());
		} else {
			map.put("userName", "");
			user.setUserName("");
		}
		int count = userService.countUser(user);
		List<User> list = userService.listPageUser(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(list, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存用户信息
	 * @param user 用户信息实体类
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveUser")
	public void saveUser(User user, HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (user.getUid() == null || "".equals(user.getUid())) {
			user.setStatus("在职");
			isSuccess = userService.insertUser(user);
		} else {
			isSuccess = userService.updateUser(user);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除用户
	 * @param str uid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteUser")
	public void deleteUser(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = userService.deleteUser(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除用户
	 * @param str uid组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteUserOfVerify")
	public void deleteUserOfVerify(@RequestParam("str") String str, HttpServletResponse response) throws Exception{
		int isSuccess = userService.deleteUserOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 获取用户已赋予的角色
	 * @param uid 用户ID
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/getRoleByUser")
	public void getRoleByUser(@RequestParam("uid") String uid, HttpServletResponse response) throws Exception{
		List<RoleBean> roles = userService.getRoleByUser(Long.parseLong(uid));
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(roles);
		ResponseUtil.write(response, jsonArray);
	}
	
	/**
	 * 用户授权
	 * @param rids 角色ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/accreditUser")
	public void accreditUser(@RequestParam("rids") String rids,@RequestParam("uid") String uid, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = userService.accreditUser(rids, uid);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 修改密码
	 * @param password 原始密码
	 * @param newPassword 新密码
	 * @param response
	 * @param session
	 * @throws Exception
	 */
	@RequestMapping(value="/setPassword")
	public void setPassword(String password, String newPassword, HttpServletResponse response, HttpSession session) throws Exception{
		int isSuccess = 0;
		User user = (User) session.getAttribute("currentUser");
		if (MD5.md5Encode(password).equals(user.getPassword())) {
			user.setPassword(MD5.md5Encode(newPassword));
			isSuccess = userService.updateUser(user);
		} else {
			isSuccess = 2;
		}
		ResponseUtil.write(response, isSuccess);
	}
}
